package com.rn.ddd.domain.merchant.service.impl;

import com.rn.ddd.domain.merchant.entity.Merchant;
import com.rn.ddd.domain.merchant.repository.MerchantRepository;
import com.rn.ddd.domain.merchant.service.MerchantDomainService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URL;
import java.util.Objects;

@Service
public class MerchantDomainServiceImpl implements MerchantDomainService {

    @Resource
    private MerchantRepository merchantRepository;

    @Override
    public Merchant getById(Long merchantId) {
        // 领域服务用于协调多个领域对象共同完成某个业务操作

        // 1. 获取商户对象(聚合根实体)
        Merchant merchant = merchantRepository.getById(merchantId);
        if (merchant == null) {
            return null;
        }
        // 2. 获取商户门店信息(实体)
        merchant.setStores(null);
        // 3. 从配置文件中读取 商户的固定费率信息(值对象)
        try {
            URL file = Objects.requireNonNull(getClass().getClassLoader().getResource("rate_vo.json"));
            merchant.setRates(null);
        } catch (Exception e) {
            // 读取或解析失败 ignore
        }
        return merchant;
    }
}
